package com.wrox;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.wrox.locator.HibernateUtil;

public class HibenateEmployeeData {
	 public static boolean addEmployee(Employee employee){
	      Session session = HibernateUtil.getSessionFactory().openSession();
	      Transaction tx = null;
	      boolean isAddEmployee=false;
	      try{
	         tx = session.beginTransaction();
	         
	        session.saveOrUpdate(employee); 
	         tx.commit();
	         isAddEmployee=true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return isAddEmployee;
	   }
	   /* Method to  READ all the employees */
	 /*  public void listEmployees( ){
	      Session session = factory.openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         List employees = session.createQuery("FROM Employee").list(); 
	         for (Iterator iterator = 
	                           employees.iterator(); iterator.hasNext();){
	            Employee employee = (Employee) iterator.next(); 
	            System.out.print("First Name: " + employee.getFirstName()); 
	            System.out.print("  Last Name: " + employee.getLastName()); 
	            System.out.println("  Salary: " + employee.getSalary()); 
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	   }*/
	   /* Method to UPDATE salary for an employee */
	   public static boolean updateEmployee(String userName ){
	      Session session = HibernateUtil.getSessionFactory().openSession();
	      Transaction tx = null;
	      Employee emp=null;
	      boolean isupdateEmployee=false;
	      try{
	         tx = session.beginTransaction();
	         
	         /*emp.setSalary( salary );*/
			 session.update(emp); 
	         tx.commit();
	        isupdateEmployee=true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return isupdateEmployee;
	   }
	   /* Method to DELETE an employee from the records */
	   public static boolean deleteEmployee(Employee employee){
	      Session session = HibernateUtil.getSessionFactory().openSession();
	      Transaction tx = null;
	      Employee emp=null;
	      boolean isDeletedEmployee=false;
	      try{
	         tx = session.beginTransaction();
	     
	         session.delete(employee); 
	         tx.commit();
	         isDeletedEmployee=true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return  isDeletedEmployee;
	   }
	   public static void getEmployee(Employee employee){
		      Session session = HibernateUtil.getSessionFactory().openSession();
		      Transaction tx = null;
		      try{
		         tx = session.beginTransaction();
		          
		         session.delete(employee); 
		         tx.commit();
		      }catch (HibernateException e) {
		         if (tx!=null) tx.rollback();
		         e.printStackTrace(); 
		      }finally {
		         session.close(); 
		      }
		   }
	}


